package moodish.server;



import java.util.Iterator;
import java.util.LinkedList;

import moodish.comm.ServerComm;
import moodish.comm.ServerSideMessage;
import moodish.comm.ServerSideMessage.Type;

public class Server implements MoodishServer {

	private ServerComm serverComm;
	private LinkedList<ServerClient> users = new LinkedList<ServerClient>();

	@Override
	public void start(ServerComm serverComm) {
		this.serverComm = serverComm;

		serverComm.start();

		try {
			fludingNetwork();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void fludingNetwork() throws InterruptedException{
		while(true){
			Thread.sleep(1000);

			if(serverComm.hasNextMessage()){
				
				ServerSideMessage m = serverComm.getNextMessage();
				
				if(m.getType() == Type.CLIENT_CONNECTED){
					
					serverComm.sendClientConnected(m.getPayload(), m.getClientNickname());
					if(isNewClient(m.getClientNickname())){
						users.add(new ServerClient(m.getClientNickname()));
						System.out.println("Server added new Client = " + m.getClientNickname() );
						System.out.println(" Number Of Clients = " + users.size());
					}else{
						//TODO load friends from user
					}

				}
				else if(m.getType() == Type.MOODISH_MESSAGE){
					sendMessageToFriends(m, m.getClientNickname(), users);
				}
				else if(m.getType() == Type.FRIENDSHIP){
					setFriendShip(m);
				}
				else if(m.getType() == Type.UNFRIENDSHIP){
					setUnfriendShip(m);
				}
				else if(m.getType() == Type.CLIENT_DISCONNECTED){
					serverComm.sendClientDisconnected(m.getPayload(), m.getClientNickname());
				}
				System.out.println("@SERVER Module@" + "GET MSG FROM: "  +m.getClientNickname()+" ,payload= "+m.getPayload());
				System.out.println("***** Message Type *****  " + m.getType());
			}	
		}
	}

	private void sendMessageToFriends(ServerSideMessage m, String senderName, LinkedList<ServerClient> list){
		serverComm.sendMoodishMessage(m.getClientNickname(), m.getClientNickname(), m.getPayload());

		for(int i = 0; i != list.size(); i++){
			if(list.get(i).getNickName().equals(senderName)){
				send(m, list.get(i).getListOfFriends(), senderName);
			}
		}
	}

	private void send(ServerSideMessage m, LinkedList<ServerClient> friends, String fromNickName){
		for(int i = 0; i < friends.size(); i++){
			serverComm.sendMoodishMessage(fromNickName, friends.get(i).getNickName(), m.getPayload());
		}
	}

	private void setFriendShip(ServerSideMessage m){
		serverComm.sendNewFriendship(m.getPayload(), m.getClientNickname());

		for(int i = 0; i < users.size(); i++){
			if(users.get(i).getNickName().equals(m.getPayload())){
				users.get(i).getListOfFriends().add(new ServerClient(m.getPayload()));
			}
		}
	}

	private void setUnfriendShip(ServerSideMessage m){
		serverComm.sendNewUnfriendship(m.getPayload(), m.getClientNickname());
		Iterator<ServerClient> it = users.iterator();

		while(it.hasNext()){
			if(it.next().equals(m.getPayload())){
				it.remove();
			}
		}
	}

	private boolean isNewClient(String nickName){
		for (ServerClient c : users) {
			if(c.getNickName().equals(nickName)){
				return false;
			}
		}
		return true;
	}
}
